import router from "@/routers";
import { defineStore } from "pinia";
import { getUrlWithParams } from "@/utils";
import { useKeepAliveStore } from "./keepAlive";
import { TabsState, TabsMenuProps } from "@/stores/interface";
import { getLocationParams } from "@/utils/index";
import { useAuthStore } from "@/stores/modules/auth";
import piniaPersistConfig from "@/stores/helper/persist";

const keepAliveStore = useKeepAliveStore();

export const useTabsStore = defineStore({
	id: "geeker-tabs",
	state: () : TabsState => ({
		tabsMenuList: []
	}),
	actions: {
		// Add Tabs
		async addTabs(tabItem : TabsMenuProps,val) {
			console.log(val)
			console.log(tabItem)
			const openValue = getLocationParams(tabItem.path,'open');
			const tabsParamsName = getLocationParams(tabItem.path,'name');
			
			const allPathIndex = this.tabsMenuList.findIndex(item => item?.path === decodeURIComponent(tabItem.path?.replace(/\+/g, '%20')))
			
			tabItem.path = decodeURIComponent(tabItem.path?.replace(/\+/g, '%20'));
			console.log(allPathIndex)
			if (allPathIndex == -1) {
				const tabIndex = this.tabsMenuList.findIndex(item => item?.path.split('?')[0] === tabItem.path.split('?')[0])
				if(tabIndex != -1){
					if(openValue == 'true'){
						if(tabsParamsName !== false){
							tabItem.joinName = tabsParamsName;
						}
						this.tabsMenuList.push(tabItem);
					} else {
						if(tabsParamsName !== false){
							this.tabsMenuList[tabIndex].joinName = tabsParamsName;
						}
						this.tabsMenuList[tabIndex].path = tabItem.path
					}
				} else {
					if(tabsParamsName !== false){
						tabItem.joinName = tabsParamsName;
					}
					this.tabsMenuList.push(tabItem);
				}
			} else {
				if(tabItem.name == 'home'){
					this.tabsMenuList[allPathIndex] = tabItem
				}
				// console.log(tabItem.path == this.tabsMenuList[allPathIndex].path)
				// console.log(tabItem.path)
			}
			// add keepalive
			if (!keepAliveStore.keepAliveName.includes(tabItem.name) && tabItem.is_keep_alive) {
				keepAliveStore.addKeepAliveName(tabItem.name);
			}
			// console.log(this.tabsMenuList)
		},
		// Remove Tabs
		async removeTabs(tabPath : string, isCurrent : boolean = true) {
			if (isCurrent) {
				this.tabsMenuList.forEach((item, index) => {
					if (item.path !== tabPath) return;
					const nextTab = this.tabsMenuList[index + 1] || this.tabsMenuList[index - 1];
					if (!nextTab) return;
					router.push(nextTab.path);
				});
			}
			// remove keepalive
			const tabItem = this.tabsMenuList.find(item => item.path === tabPath);
			tabItem?.is_keep_alive && keepAliveStore.removeKeepAliveName(tabItem.name);
			// set tabs
			this.tabsMenuList = this.tabsMenuList.filter(item => item.path !== tabPath);
		},
		// Close Tabs On Side
		async closeTabsOnSide(path : string, type : "left" | "right") {
			const currentIndex = this.tabsMenuList.findIndex(item => item.path === path);
			if (currentIndex !== -1) {
				const range = type === "left" ? [0, currentIndex] : [currentIndex + 1, this.tabsMenuList.length];
				this.tabsMenuList = this.tabsMenuList.filter((item, index) => {
					return index < range[0] || index >= range[1] || !item.close;
				});
			}
			// set keepalive
			const KeepAliveList = this.tabsMenuList.filter(item => item.is_keep_alive);
			keepAliveStore.setKeepAliveName(KeepAliveList.map(item => item.name));
		},
		// Close MultipleTab
		async closeMultipleTab(tabsMenuValue ?: string) {
			this.tabsMenuList = this.tabsMenuList.filter(item => {
				return item.path === tabsMenuValue || !item.close;
			});
			// set keepalive
			const KeepAliveList = this.tabsMenuList.filter(item => item.is_keep_alive);
			keepAliveStore.setKeepAliveName(KeepAliveList.map(item => item.name));
		},
		// Set Tabs
		async setTabs(tabsMenuList : TabsMenuProps[]) {
			this.tabsMenuList = tabsMenuList;
		},
		// Set Tabs Title
		async setTabsTitle(title : string) {
			this.tabsMenuList.forEach(item => {
				if (item.path == getUrlWithParams()) item.title = title;
			});
		}
	},
	persist: piniaPersistConfig("geeker-tabs")
});